package com.bgthrid;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kldy8.kldyw.R;
import com.kldy8.kldyw.bean.New;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by florentchampigny on 24/04/15.
 */
public class TestRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static interface OnRecyclerViewListener
    {
              void onItemClick(int position);
              boolean onItemLongClick(int position);
    }

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener)
    {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private OnRecyclerViewListener onRecyclerViewListener;
    ImageLoader loader;

    List<Object> contents;
    Context mContents;

    private int myType = 0;
    static final int TYPE_HEADER = 0;
    static final int TYPE_CELL = 1;

    public final static int TYPE_NEWS = 0;

    public TestRecyclerViewAdapter(Context context, List<Object> contents, int type)
    {
        myType = type;
        this.contents = contents;
        mContents = context;
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    public void addList( List<Object> list)
    {
        contents.addAll(list);
    }

    @Override
    public int getItemViewType(int position) {
        switch (position)
        {
            case 0:
                return TYPE_HEADER;
            default:
                return TYPE_CELL;
        }
    }

    @Override
    public int getItemCount() {
        return contents.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEADER: {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item_card_big, parent, false);

                return new MyViewHolder(view);
            }
            case TYPE_CELL: {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item_card_small, parent, false);
                return new MyViewHolder(view);
            }
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.position = position;
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                String title = null;
                String imageUrl = null;
                String subtitle = null;
                String supporting = null;
                String id = null;

                if (myType == TYPE_NEWS)
                {
                    New my_new = (New) contents.get(position);
                    id = my_new.title;
                    title = my_new.title;
                    imageUrl = my_new.cover_pic;
//                    subtitle = my_new.title;
                    supporting = my_new.summary;

                }
                loader.displayImage(imageUrl, myViewHolder.image);
                myViewHolder.titleTextView.setText(title);
//              myViewHolder.subtitleTextView.setText(supporting);
                myViewHolder.supportingTextView.setText(supporting);

                break;
            case TYPE_CELL:
                String titleII = null;
                String imageUrlII = null;
                String subtitleII = null;
                String supportingII = null;
                String idII = null;

                if (myType == TYPE_NEWS)
                {
                    New my_new = (New) contents.get(position);
                    idII = my_new.title;
                    titleII = my_new.title;
                    imageUrlII = my_new.cover_pic;
//                  subtitleII = my_new.summary;
                    supportingII = my_new.summary;

                }
                loader.displayImage(imageUrlII, myViewHolder.image);
                myViewHolder.titleTextView.setText(titleII);
                myViewHolder.subtitleTextView.setText(supportingII);
//                myViewHolder.supportingTextView.setText(supportingII);
                break;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener, View.OnLongClickListener
    {
        int position;
        ImageView image ;
        TextView titleTextView  ;
        TextView subtitleTextView  ;
        TextView supportingTextView ;


        public MyViewHolder(View itemView)
        {
            super(itemView);
            image  = (ImageView) itemView.findViewById(R.id.image);
            titleTextView  = (TextView) itemView.findViewById(R.id.title);
            subtitleTextView  = (TextView) itemView.findViewById(R.id.subtitle);
            supportingTextView  = (TextView) itemView.findViewById(R.id.supportingText);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v)
        {
            if (null != onRecyclerViewListener)
            {
                onRecyclerViewListener.onItemClick(position);
            }
        }

      @Override
        public boolean onLongClick(View v)
      {
                  if(null != onRecyclerViewListener)
                  {
                           return onRecyclerViewListener.onItemLongClick(position);
                   }
                 return false;
               }
    }

}